﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RclSystem
{
    public partial class SaleStatementDetail : UIForm
    {
        private string salesman = "";
        private string sdate = "";
        private string edate = "";
        List<StatementAccountVo> sortedVolist = new List<StatementAccountVo>();
        //记录业务员按单位汇总信息
        Dictionary<string, StatementAccountVo> hashTable = new Dictionary<string, StatementAccountVo>();

        public SaleStatementDetail(string salesman,string sdate,string edate)
        {
            InitializeComponent();
            this.salesman = salesman;
            this.sdate = sdate;
            this.edate = edate;
            this.Text = "业务员对账明细[" + salesman + "]";
        }

        private void queryAccounts()
        {
            sortedVolist = new List<StatementAccountVo>();
            hashTable = new Dictionary<string, StatementAccountVo>();
            uiDataGridView1.Rows.Clear();
            List<StatementAccountVo> volist = new List<StatementAccountVo>();

            //获取发货单列表
            DataTable dt_deliver = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_delivery where salesman = '" + salesman + "' and deliverdate >= '" + sdate
                    + "' and deliverdate <= '" + edate + "' order by deliverdate");

            if (dt_deliver.Rows.Count > 0)
            {
                foreach (DataRow row in dt_deliver.Rows)
                {
                    if (hashTable.ContainsKey(row["companyname"].ToString()))
                    {
                        StatementAccountVo vo = hashTable[row["companyname"].ToString()];
                        vo.count += double.Parse(row["salecount"].ToString());
                        vo.deliverprice += double.Parse(row["saleamount"].ToString());
                        vo.balance += double.Parse(row["saleamount"].ToString());
                        hashTable[row["companyname"].ToString()] = vo;
                    }
                    else
                    {
                        StatementAccountVo vo = new StatementAccountVo();
                        vo.companyname = row["companyname"].ToString();
                        vo.optdate = (DateTime)row["deliverdate"];
                        vo.count = double.Parse(row["salecount"].ToString());
                        vo.deliverprice = double.Parse(row["saleamount"].ToString());
                        vo.balance += double.Parse(row["saleamount"].ToString());
                        hashTable.Add(row["companyname"].ToString(), vo);
                    }
                       
                }
            }

            //获取收款单列表
            DataTable dt_receipt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_receipt where salesman = '" + salesman + "' and  receiptdate >= '" + sdate
                        + "' and receiptdate <= '" + edate + "'");
            //期初数结算金额
            double initaljs = 0;
            if (dt_receipt.Rows.Count > 0)
            {
                foreach (DataRow row in dt_receipt.Rows)
                {
                    if (row["parentid"].ToString() != "0")
                    {
                        if (hashTable.ContainsKey(row["companyname"].ToString()))
                        {
                            StatementAccountVo vo = hashTable[row["companyname"].ToString()];
                            vo.receiptprice += double.Parse(row["receiptamount"].ToString());
                            vo.balance -= double.Parse(row["receiptamount"].ToString());
                            hashTable[row["companyname"].ToString()] = vo;
                        }
                        else
                        {
                            StatementAccountVo vo = new StatementAccountVo();
                            vo.companyname = row["companyname"].ToString();
                            vo.optdate = (DateTime)row["receiptdate"];
                            vo.receiptprice = double.Parse(row["receiptamount"].ToString());
                            vo.balance -= double.Parse(row["receiptamount"].ToString());
                            hashTable.Add(row["companyname"].ToString(), vo);
                        }
                    }                    
                }
            }

            //判断是否有期初数
            DataTable dt_saleman = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_saleman where name = '" + salesman + "'");
            if (dt_saleman.Rows.Count > 0)
            {
                //获取单位信息
                DataTable dt_company = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where parentid = " + dt_saleman.Rows[0]["id"].ToString());
                if (dt_company.Rows.Count > 0)
                {
                    foreach (DataRow row in dt_company.Rows)
                    {
                        initaljs = double.Parse(row["initalamount"].ToString());
                        if (hashTable.ContainsKey(row["name"].ToString()))
                        {
                            StatementAccountVo vo = hashTable[row["name"].ToString()];
                            vo.deliverprice += double.Parse(row["initalamount"].ToString());
                            vo.balance += double.Parse(row["initalamount"].ToString());
                            hashTable[row["name"].ToString()] = vo;
                        }
                        else
                        {
                            StatementAccountVo vo = new StatementAccountVo();
                            vo.companyname = row["name"].ToString();
                            vo.deliverprice += double.Parse(row["initalamount"].ToString());
                            vo.balance += double.Parse(row["initalamount"].ToString());
                            hashTable.Add(row["name"].ToString(), vo);
                        }

                    }
                }   
            }
            else
            {
                this.ShowErrorDialog2("查询业务员信息异常.");
                return;
            }

            int index = 0;
            double count_total = 0;
            double deliverprice_total = 0;
            double receiptprice_total = 0;
            double balance_total = 0;
            foreach (KeyValuePair<string, StatementAccountVo> kvp in hashTable)
            {
                index = this.uiDataGridView1.Rows.Add();
                this.uiDataGridView1.Rows[index].Cells[0].Value = index + 1;
                this.uiDataGridView1.Rows[index].Cells[1].Value = kvp.Value.companyname;
                this.uiDataGridView1.Rows[index].Cells[2].Value = kvp.Value.count;
                this.uiDataGridView1.Rows[index].Cells[3].Value = Math.Round(kvp.Value.deliverprice, 2).ToString();
                this.uiDataGridView1.Rows[index].Cells[4].Value = Math.Round(kvp.Value.receiptprice, 2).ToString();
                this.uiDataGridView1.Rows[index].Cells[5].Value = Math.Round(kvp.Value.balance, 2).ToString();

                count_total += kvp.Value.count;
                deliverprice_total += kvp.Value.deliverprice;
                receiptprice_total += kvp.Value.receiptprice;
                balance_total += kvp.Value.balance;
            }
            //生产合计行
            index = this.uiDataGridView1.Rows.Add();
            this.uiDataGridView1.Rows[index].Cells[0].Value = index + 1;
            this.uiDataGridView1.Rows[index].Cells[1].Value = "合计";
            this.uiDataGridView1.Rows[index].Cells[2].Value = Math.Round(count_total, 3).ToString();
            this.uiDataGridView1.Rows[index].Cells[3].Value = Math.Round(deliverprice_total, 2).ToString();
            this.uiDataGridView1.Rows[index].Cells[4].Value = Math.Round(receiptprice_total, 2).ToString();
            this.uiDataGridView1.Rows[index].Cells[5].Value = Math.Round(balance_total, 2).ToString();
            //添加总期初数行
            /*StatementAccountVo initalvo = new StatementAccountVo();
            initalvo.companyname = "期初数";
            initalvo.optdate = DateTime.Parse("0001-01-01");
            initalvo.deliverprice = initaljs;
            volist.Add(initalvo);

            //对清单进行排序-按日期升序
            sortedVolist = volist.OrderBy(p => p.optdate).ToList();
            )          //显示
            int index = 0;
            double count_total = 0;
            double deliverprice_total = 0;
            double receiptprice_total = 0;
            foreach (StatementAccountVo vo in sortedVolist)
            {
                index = this.uiDataGridView1.Rows.Add();
                this.uiDataGridView1.Rows[index].Cells[0].Value = index + 1;
                this.uiDataGridView1.Rows[index].Cells[1].Value = vo.companyname;
                this.uiDataGridView1.Rows[index].Cells[2].Value = vo.optdate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : vo.optdate.ToString("yyyy-MM-dd");
                this.uiDataGridView1.Rows[index].Cells[3].Value = vo.count.ToString() == "0" ? "" : vo.count.ToString();
                this.uiDataGridView1.Rows[index].Cells[4].Value = vo.deliverprice.ToString() == "0" ? "" : vo.deliverprice.ToString();
                this.uiDataGridView1.Rows[index].Cells[5].Value = vo.receiptprice.ToString() == "0" ? "" : vo.receiptprice.ToString();

                count_total += vo.count;
                deliverprice_total += vo.deliverprice;
                receiptprice_total += vo.receiptprice;
            }
            //生成合计行
            index = this.uiDataGridView1.Rows.Add();
            this.uiDataGridView1.Rows[index].Cells[0].Value = index + 1;
            this.uiDataGridView1.Rows[index].Cells[1].Value = "合计";
            this.uiDataGridView1.Rows[index].Cells[2].Value = "";
            this.uiDataGridView1.Rows[index].Cells[3].Value = Math.Round(count_total, 3).ToString();
            this.uiDataGridView1.Rows[index].Cells[4].Value = Math.Round(deliverprice_total, 2).ToString();
            this.uiDataGridView1.Rows[index].Cells[5].Value = Math.Round(receiptprice_total, 2).ToString();
            */
        }

        private void SaleStatementDetail_Load(object sender, EventArgs e)
        {
            queryAccounts();
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "btn_detail" && e.RowIndex >= 0)
            {
                if (uiDataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() == "合计")
                {
                    return;
                }
                DataGridViewRow vr = uiDataGridView1.Rows[e.RowIndex];
                string companayname = vr.Cells[1].Value.ToString();
                //明细对话框
                SaleStatementDetail_Company sdetail = new SaleStatementDetail_Company(this.salesman,companayname, sdate, edate);
                sdetail.ShowDialog();
            }
        }

        private void uiDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                // 检查是否是按钮列
                if (uiDataGridView1.Columns[e.ColumnIndex].Name == "btn_detail")
                {
                    if (e.ColumnIndex == 6 && uiDataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() == "合计")
                    {
                        // 跳过绘制按钮
                        e.Handled = true;
                    }
                }
            }
        }
    }
}
